package com.circle.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.circle.model.entity.AjaxJson;
import com.circle.model.entity.DccInternalFileEntity;
import com.circle.model.entity.DccOutFileEntity;
import com.circle.model.module.ConfigData;
import com.circle.model.module.OkPost;
import com.circle.model.module.log.LoggerABC;
import com.circle.model.module.log.LoggerBuilder;
import com.circle.model.util.ConfigDataFactory;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.Map;

public class ExportAttachService {

    public void export(DccInternalFileEntity internalFile, ConfigData configData,String chapter) {
        LoggerABC loggerABC = LoggerBuilder.build();
        try{
            AjaxJson returnJson = new OkPost(configData.getUrl() + "dccSpecAttachController.do?getPathByInternalid")
                    .addFormParam("internalid", internalFile.getId())
                    .addFormParam("chapter", chapter)
                    .addCookie("DCC_TOKEN", configData.getToken())
                    .execute();
            if (!returnJson.isSuccess()) {
                return;
            }
            File interFile = new File(configData.getExportDir(), internalFile.getSpecNo());
            if (!interFile.exists()) {
                FileUtils.forceMkdir(interFile);
            }
            File attach = new File(interFile, chapter);
            if (!attach.exists()) {
                FileUtils.forceMkdir(attach);
            }
            JSONArray dataArrary = (JSONArray) returnJson.getObj();
            for (int i = 0;i<dataArrary.size();i++) {
                JSONObject jsonObject = dataArrary.getJSONObject(i);
                String path = jsonObject.getString("filepath");
                File srcFile = new File(path);
                if (!srcFile.exists()) {
                    loggerABC.logMessage(LoggerABC.WARN, "Error occurred during ExportAttachService export: File system element for parameter 'source' does not exist: " + srcFile.getAbsolutePath());
                    continue;
                }
                File destFile = new File(attach, jsonObject.getString("attachNo") + "_" + jsonObject.getString("realname"));
                FileUtils.copyFile(srcFile, destFile, true); // 第三个参数为true表示复制时保留文件属性
            }

        }catch (Exception e){
            loggerABC.setException(e);
            loggerABC.logMessage(LoggerABC.ERROR, "Error occurred during ExportAttachService export: " + e.getMessage());
        }
    }


    public void exportAttForOutFile(DccOutFileEntity outFile, ConfigData configData) {
        LoggerABC loggerABC = LoggerBuilder.build();
        try{
            //http://dccdev.circle.com:8080/dcc/dccFolderController.do?getPathByPdocid&pdocid=abcdedf
            AjaxJson returnJson = new OkPost(configData.getUrl() + "dccFolderController.do?getPathByPdocid")
                    .addFormParam("pdocid", outFile.getId())
                    .addCookie("DCC_TOKEN", configData.getToken())
                    .execute();
            if (!returnJson.isSuccess()) {
                return;
            }
            File otFile = new File(configData.getExportDir(), outFile.getSpecNo());
            if (!otFile.exists()) {
                FileUtils.forceMkdir(otFile);
            }

            JSONArray dataArrary = (JSONArray) returnJson.getObj();
            for (int i = 0;i<dataArrary.size();i++) {
                JSONObject jsonObject = dataArrary.getJSONObject(i);

                String atttype=jsonObject.getString("webuploader");
                if("outfileattach".equals(atttype)){
                    String path = jsonObject.getString("filepath");
                    File srcFile = new File(path);
                    if (!srcFile.exists()) {
                        loggerABC.logMessage(LoggerABC.WARN, "Error occurred during ExportAttachService export: File system element for parameter 'source' does not exist: " + srcFile.getAbsolutePath());
                        continue;
                    }
                    File destFile = new File(otFile,jsonObject.getString("realname"));
                    FileUtils.copyFile(srcFile, destFile, true); // 第三个参数为true表示复制时保留文件属性
                }


            }

        }catch (Exception e){
            loggerABC.setException(e);
            loggerABC.logMessage(LoggerABC.ERROR, "Error occurred during ExportAttachService export: " + e.getMessage());
        }
    }

}
